package com.hzdl.house_mgr.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class ExlExportUtil {

    /**
     * 2.生成excel文档，保存到磁盘
     */
    public static void createExcelToFile(){

        // 创建Excel文档对象
        HSSFWorkbook workbook = createExcel();

        try {
            /**
             * 导出所以用到输出流
             * 参数为输出的地址
             */
            FileOutputStream fout = new FileOutputStream("E:\\poi.xls");

            workbook.write(fout);   //Workbook提供了write的方法
            fout.close();           //将输出流关闭
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static HSSFWorkbook createExcel(){

        // 创建Excel文档对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFCellStyle style = workbook.createCellStyle();

        // 创建sheet对象
        HSSFSheet sheet1 = workbook.createSheet();
        // 设置单元格默认宽高
        sheet1.setDefaultRowHeight((short)(20*20));
        sheet1.setDefaultColumnWidth(10);

        // 创建第一行
        HSSFRow row1 = sheet1.createRow(0);
        // 创建第一列
        HSSFCell row1_cell1 = row1.createCell(0);
        row1_cell1.setCellValue("编号");
        HSSFFont hssfFont = workbook.createFont();
        hssfFont.setFontName("幼圆");
        hssfFont.setColor(Font.COLOR_RED);
        hssfFont.setFontHeight((short)(30*30));
        style.setFont(hssfFont);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 修改单元格样式
        row1_cell1.setCellStyle(style);

        // 创建第二列
//        HSSFCell row1_cell2 = row1.createCell(1);
//        row1_cell2.setCellValue("姓名");

        // 合并单元格
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 6);
        sheet1.addMergedRegion(cellRangeAddress);

        return  workbook;

    }


    public static ByteArrayOutputStream createExcelToStream() {
        HSSFWorkbook excel = createExcel();
        // 输出到ByteArrayOutputStream 对象中
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            excel.write(baos);
//            baos.flush();
//            baos.close();
            return baos;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取模板excel文件
     * @param template
     */
    public static ByteArrayOutputStream readExcel(String template){
        try {
            // 获取文件的输入流对象
            InputStream resourceAsStream = ExlExportUtil.class.getClassLoader().getResourceAsStream(template);

            // 创建Workbook对象
            HSSFWorkbook workbook = new HSSFWorkbook(resourceAsStream);


            // 获取sheet对象
            HSSFSheet sheet = workbook.getSheetAt(0);


            // 获取行对象
            HSSFRow row1 = sheet.getRow(0);
            // 获取列对象
            HSSFCell row1_cell1 = row1.getCell(0);
            row1_cell1.setCellValue("张三的出租单");

            HSSFRow row2 = sheet.getRow(1);
            HSSFCell row2_cell2 = row2.getCell(1);
            row2_cell2.setCellValue("CZD_8373636_99847_000_00000");


            // 获取二维码对象
            BufferedImage image = ZXingCode.createZXingCodeToStream("CZD_8373636_99847_000_00000", 300, 300);
            // 把二维码转换成输出流对象
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ImageIO.write(image , "JPEG", stream);
            //画图的顶级管理器，一个sheet只能获取一个
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            /**
             * 参数4  设置图片的平铺程度  最大值是255  255代表铺满当前单元格 小于255就铺不满
             * 参数5  列的开始坐标
             * 参数6  行的开始坐标
             * 参数7  列的结束坐标
             * 参数8  行的结束坐标
             */
            HSSFClientAnchor hssfClientAnchor = new HSSFClientAnchor(0,0,0,255,(short) 4,1,(short)5,1);

            hssfClientAnchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
            int index = workbook.addPicture(stream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG);
            patriarch.createPicture(hssfClientAnchor, index);


            HSSFCell row2_cell4 = row2.getCell(3);
//            row2_cell4.setCellValue("");

            // TODO .....

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            workbook.write(baos);

            return baos;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return  null;

    }

    public static void main(String[] args) {
        createExcelToFile();
    }
}
